package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.RechargeConfigureBO;
import com.junzhiit.nengji.cms.service.RechargeConfigureService;
import com.junzhiit.nengji.cms.web.exception.RechargeConfigureIsExistException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.RechargeConfigureVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.RechargeConfigureDAO;
import com.junzhiit.nengji.model.RechargeConfigure;

@Service
public class RechargeConfigureServiceImpl extends BaseServiceImpl implements RechargeConfigureService {

	@Autowired
	private RechargeConfigureDAO rechargeConfigureDAO;
	
	@Override
	public ResultVO add(RechargeConfigureVO rc) {
		ResultVO result = new ResultVO();
		RechargeConfigure rechargeConfigure = rechargeConfigureDAO.getByRechargeMoney(rc.getRechargeMoney(), rc.getGiveType());
		if(rechargeConfigure!=null){
			throw new RechargeConfigureIsExistException("已经存在该充值金额的配置。");
		}
		rechargeConfigure = new RechargeConfigure();
		BeanUtils.copyProperties(rc, rechargeConfigure);
		result.setData(rechargeConfigureDAO.insert(rechargeConfigure));
		return result;
	}

	@Override
	public ResultVO update(RechargeConfigureVO rc) {
		ResultVO result = new ResultVO();
		RechargeConfigure rechargeConfigure = new RechargeConfigure();
		BeanUtils.copyProperties(rc, rechargeConfigure);
		result.setData(rechargeConfigureDAO.update(rechargeConfigure));
		return result;
	}

	@Override
	public ResultVO delete(String Id) {
		ResultVO result = new ResultVO();
		RechargeConfigure rechargeConfigure = new RechargeConfigure();
		rechargeConfigure.setId(Id);
		result.setData(rechargeConfigureDAO.delete(rechargeConfigure));
		return result;
	}

	@Override
	public Map<String, Object> findAll(RechargeConfigureVO vo,DataTablesParamVO dtp) {
		RechargeConfigureBO rechargeConfigure = new RechargeConfigureBO();
		BeanUtils.copyProperties(vo, rechargeConfigure);
		int totalCount = rechargeConfigureDAO.getTotalCount(rechargeConfigure);
		List<RechargeConfigure> list = rechargeConfigureDAO.findAll(rechargeConfigure, dtp.getStart(), dtp.getLength());
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		result.setData(rechargeConfigureDAO.query(id));
		return result;
	}

}
